package ua.luxoft.parse;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;

import ua.luxoft.file.FindFile;
import ua.luxoft.fileinfo.FileInfo;
import ua.luxoft.iface.iParseFiles;

public class WorkWithFile implements iParseFiles{
	public static final String PATH = "/home/user/Documents/MyProjects/example";
	@Override
	public String get(String key) {
		HashMap<String, FileInfo> files = new HashMap<String, FileInfo>();
		FindFile find = new FindFile();
		ParseFile parse = new ParseFile();
		FileInfo info = new FileInfo();
		files = parse.workWithFile(find.getAllFilesInDir(PATH));
		info = (FileInfo)files.get(key);
		
		return readSomeFile(info.getFileName(), info.getStartRead(), info.getEndRead());
		 
	}
	
	private String readSomeFile(String fileName, long pointerStart, long pointerEnd){
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(PATH + fileName, "r");
		} catch (FileNotFoundException e1) {
			System.out.println("Problem in reading");
		}
		byte [] b = null;
		try {
			raf.read(b, (int)pointerStart, (int)(pointerEnd - pointerStart));
		} catch (IOException e) {
			System.out.println("problem in readfile");
			e.printStackTrace();
		}
		return b.toString();
	}
}
